package com.lanqiao.attendance.service.impl;

import com.lanqiao.attendance.handler.TeacherHandler.Secret;
import com.lanqiao.attendance.mapper.TeacherMapper;
import com.lanqiao.attendance.pojo.Teacher;
import com.lanqiao.attendance.service.TeacherService;
import com.lanqiao.attendance.utils.JWTUtils;
import com.lanqiao.attendance.utils.JWTUtils2;
import com.lanqiao.attendance.vo.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * * @Author Pu Yunsheng
 * * @Data 2022/7/9 下午 05:11
 * * @Version 1.0
 **/
@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource(name = "teacherMapper")
    TeacherMapper teacherMapper;

    @Override
    @Secret  //密码加密
    public Result login(LoginParam2 login) {
        Result result = null;
        Teacher teacher = teacherMapper.login(login);
        System.out.println(login.getPassword());
        System.out.println(teacher);
        if(teacher != null){
            TeacherVo teacherVo = new TeacherVo(teacher);
            String token = JWTUtils.createToken(1L,"老师");
            teacherVo.setToken(token);
            result = Result.success("登录成功！",teacherVo);
        }
        else{
            result = Result.fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(),ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        return result;
    }

    @Override
    public Result register(TeacherVo teacherVo) {
        /**
         * 1、注册，先判断是否存在该账户。
         * 2、存在不让注册，返回账户已存在，
         * 3、否咋让其注册。返回注册对的信息。包括，数据库赋予的代理id
         */
        Teacher teacher = teacherMapper.queryTeacherByNum(teacherVo.getAccount());
        if(teacher != null){
            return Result.fail(ErrorCode.ACCOUNT_EXIST_ERROR.getCode(),ErrorCode.ACCOUNT_EXIST_ERROR.getMsg());
        }
        String salt = "shzu";
        String password = DigestUtils.md5Hex(teacherVo.getPassword()+salt);
        teacherVo.setPassword(password);
        int row = teacherMapper.addTeacher(teacherVo);
        Teacher teacher1 = teacherMapper.queryTeacherByNum(teacherVo.getAccount());
        return Result.success("教师注册成功！",teacher1);
    }

    @Override
    public Result deleteTeacher(String account) {
        Result result = null;
        int row = teacherMapper.deleteTeacherByNum(account);
        if( row > 0){
            result = Result.success("删除教师成功！",null);
        }
        else{
            result = Result.fail(ErrorCode.PARAMS_ERROR.getCode(),ErrorCode.PARAMS_ERROR.getMsg());
        }
        return result;
    }

    @Override
    @Secret  //密码加密
    public Result updateTeacher(TeacherVo teacherVo) {
        Result result = null;
        int row = teacherMapper.updateTeacher(teacherVo);
        if( row > 0){
            Teacher teacher1 = teacherMapper.queryTeacherByNum(teacherVo.getAccount());
            TeacherVo teacherVo1 = new TeacherVo(teacher1);
            result = Result.success("教师信息修改成功！",teacherVo1);
        }
        else{
            result = Result.fail(ErrorCode.PARAMS_ERROR.getCode(),ErrorCode.PARAMS_ERROR.getMsg());
        }
        return result;
    }

    @Override
    public Result getTeacherByNum(String account) {
        Result result = null;
        Teacher teacher = teacherMapper.queryTeacherByNum(account);
        System.out.println(teacher);
        if(teacher != null){
            TeacherVo teacherVo = new TeacherVo(teacher);
            result = Result.success("教师信息获取成功！",teacherVo);
        }
        else {
            result = Result.fail(ErrorCode.ID_NOT_EXSITS.getCode(),"账号不存在！");
        }
        return result;
    }

    @Override
    public Result getAllTeacher() {
        Result result = null;
        List<Teacher> teachers = teacherMapper.queryAllTeacher();
        if(teachers != null){
            List<TeacherVo> teacherVoList = new ArrayList<>();
            for (Teacher teacher : teachers) {
                teacherVoList.add(new TeacherVo(teacher));
                System.out.println(teacher.getCreateTime());
            }
            result = Result.success("教师信息获取成功！",teacherVoList);
            System.out.println(result.toString());
        }
        else {
            result = Result.fail(ErrorCode.ID_NOT_EXSITS.getCode(),"账号不存在！");
        }

        System.out.println();
        return result;
    }

    @Override
    @Secret  //密码加密
    public Result addTeacher(TeacherVo teacherVo) {
        System.out.println(teacherVo.getPassword());
        Result result = null;
        Teacher teacher1 = teacherMapper.queryTeacherByNum(teacherVo.getAccount());
        if(teacher1 != null){
            return Result.fail(444,"该教师账号已存在");
        }
        int rows = teacherMapper.addTeacher(teacherVo);
        if(rows > 0){
            Teacher teacher = teacherMapper.queryTeacherByNum(teacherVo.getAccount());
            TeacherVo teacherVo1 = new TeacherVo(teacher);
            result = Result.success("添加教师成功", teacherVo1);
        }
        else {
            result = Result.fail(ErrorCode.PARAMS_ERROR.getCode(),"添加教师失败！");
        }
        return result;
    }


    /**
     * token验证
     * @param token
     * @return
     */
    @Override
    public boolean checkToken(String token) {
        Map<String, Object> stringObjectMap = JWTUtils2.checkToken(token);
        String uerId = (String) stringObjectMap.get("uerId");
        Teacher teacher = teacherMapper.queryTeacherByNum(uerId);
        if(teacher != null){
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public Result pageQuery(Integer pageNumber,Integer pageSize) {
        Result result = null;
        List<Teacher> teachers = teacherMapper.pageQuery((pageNumber-1)*pageSize, pageSize);
        if(teachers != null){
            List<TeacherVo> teacherVoList = new ArrayList<>();
            for (Teacher teacher : teachers) {
                teacherVoList.add(new TeacherVo(teacher));
            }
            result = Result.success("教师信息分页查询成功！",teacherVoList);
        }
        else {
            result = Result.fail(ErrorCode.PARAMS_ERROR.getCode(),"教师信息分页查询失败！");
        }
        return result;
    }

    @Override
    public String queryNameByAccount(String account) {
        Teacher teacher = teacherMapper.queryTeacherByNum(account);
        return teacher.getName();
    }
}
